using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using LogParser.DA.DataObjects;

namespace ASP_TF2LogParser.UserControls
{
    public partial class MatchTeamStats : System.Web.UI.UserControl
    {
        private int _logId;
        private List<LogParser.Player> _playerList = new List<LogParser.Player>();
        private LogParser.DA.PlayerModel _playerModel = new LogParser.DA.PlayerModel();


        protected void Page_Load( object sender, EventArgs e )
        {


            if ( Request[ "logId" ] != null )
                _logId = Convert.ToInt32( Request[ "logId" ].ToString() );
            else
                _logId = 1;


        }

        protected void teamStats_DataBound( object sender, EventArgs e )
        {
            LogParser.DA.DataObjects.PlayerStatsDS.PlayerStatsTeamDataTable dataTable = teamStats.DataSource as LogParser.DA.DataObjects.PlayerStatsDS.PlayerStatsTeamDataTable;

            LogParser.DA.DataObjects.PlayerStatsDS.PlayerStatsTeamDataTable totalTable = new PlayerStatsDS.PlayerStatsTeamDataTable();
            LogParser.DA.DataObjects.PlayerStatsDS.PlayerStatsTeamRow totalRow = ( LogParser.DA.DataObjects.PlayerStatsDS.PlayerStatsTeamRow ) totalTable.NewRow();


            totalRow.Assists = 0;
            totalRow.BuiltObjects = 0;
            totalRow.CaptureBlocks = 0;
            totalRow.Captures = 0;
            totalRow.Deaths = 0;
            totalRow.Destructions = 0;
            totalRow.Dominations = 0;
            totalRow.FlagsCaptured = 0;
            totalRow.FlagsDefended = 0;
            totalRow.FlagsTouched = 0;
            totalRow.KillDeathRatio = "";
            totalRow.Kills = 0;
            totalRow.PlayerId = "";
            totalRow.PlayerName = "";
            totalRow.Points = 0.0;
            totalRow.Revenges = 0;
            totalRow.Ubers = 0;

            double totalKDRatio = 0;

            foreach ( LogParser.DA.DataObjects.PlayerStatsDS.PlayerStatsTeamRow dr in dataTable )
            {


                totalRow.Assists += dr.Assists;
                totalRow.BuiltObjects += dr.BuiltObjects;

                totalRow.CaptureBlocks += dr.CaptureBlocks;
                totalRow.Captures += dr.Captures;
                totalRow.Deaths += dr.Deaths;
                totalRow.Destructions += dr.Destructions;
                totalRow.Dominations += dr.Dominations;
                totalRow.FlagsCaptured += dr.FlagsCaptured;
                totalRow.FlagsDefended += dr.FlagsDefended;
                totalRow.FlagsTouched += dr.FlagsTouched;
                totalRow.Kills += dr.Kills;
                totalRow.Points += dr.Points;
                totalRow.Revenges += dr.Revenges;
                totalRow.Ubers += dr.Ubers;
                
                totalKDRatio += Convert.ToDouble(dr.KillDeathRatio);

            }

            double totalRatio = ( ( ( double ) ( totalKDRatio ) ) / ( ( double ) dataTable.Rows.Count ) );
            totalRow.KillDeathRatio = string.Format( "{0:0.##}", totalRatio );


            totalTable.Rows.Add( totalRow );


            

            teamStatsTotal.DataSource = totalTable;
            teamStatsTotal.DataBind();

            //teamStatsTotal.Columns[ 0 ].HeaderText = "Player";
            //teamStatsTotal.Columns[ "Destructions" ].HeaderText = "Des";
            //teamStatsTotal.Columns[ "BuiltObjects" ].HeaderText = "Built";
            //teamStatsTotal.Columns[ "Captures" ].HeaderText = "Caps";
            //teamStatsTotal.Columns[ "CaptureBlocks" ].HeaderText = "Player";
            //teamStatsTotal.Columns[ "FlagsCaptured" ].HeaderText = "Player";
            //teamStatsTotal.Columns[ "FlagsTouched" ].HeaderText = "Player";
            //teamStatsTotal.Columns[ "FlagsDefended" ].HeaderText = "Player";
            //teamStatsTotal.Columns[ "Ubers" ].HeaderText = "Player";
            //teamStatsTotal.Columns[ "Dominations" ].HeaderText = "Dom";
            //teamStatsTotal.Columns[ "Revenges" ].HeaderText = "Rev";
            //teamStatsTotal.Columns[ "Dominations" ].HeaderText = "Dom";
            //teamStatsTotal.Columns[ "KillDeathRatio" ].HeaderText = "K:D";
            //teamStatsTotal.Columns[ "Backstabs" ].HeaderText = "BS";
            //teamStatsTotal.Columns[ "Headshots" ].HeaderText = "HS";
            //teamStatsTotal.Columns[ "PlayerId" ].HeaderText = "ID";

        }

        public void LoadTeam( string team )
        {
            if ( Request[ "logId" ] != null )
                _logId = Convert.ToInt32( Request[ "logId" ].ToString() );
            else
                _logId = 1;


            LogParser.Collections.TeamList _teamList = new LogParser.Collections.TeamList();
            LogParser.GameObjects.Team tempTeam = _teamList.GetTeam( team );

            LogParser.DA.DataObjects.MatchesDSTableAdapters.MatchesTableAdapter mta = LogParser.DA.TableAdapters.MatchesTableAdapter();
            LogParser.DA.DataObjects.MatchesDS.MatchesDataTable mdt = mta.MatchesByLogId( _logId );
            LogParser.DA.DataObjects.MatchesDS.MatchesRow mr = ( LogParser.DA.DataObjects.MatchesDS.MatchesRow ) ( mdt.Rows[ 0 ] );

            LogParser.DA.DataObjects.MatchPlayerListDSTableAdapters.MatchPlayerListTableAdapter mplta = LogParser.DA.TableAdapters.MatchPlayerListTableAdapter();
            MatchPlayerListDS.MatchPlayerListDataTable mpldt = mplta.MatchPlayerListByMatchIdTeamId( mr.MatchId, tempTeam.ID );

            foreach ( MatchPlayerListDS.MatchPlayerListRow dr in mpldt.Rows )
            {
                LogParser.Player tempPlayer = new LogParser.Player();
                tempPlayer.PlayerId = dr.PlayerId;
                _playerList.Add( tempPlayer );
            }

            LogParser.DA.DataObjects.PlayerStatsDS.PlayerStatsTeamDataTable dt = new LogParser.DA.DataObjects.PlayerStatsDS.PlayerStatsTeamDataTable();

            foreach ( LogParser.Player player in _playerList )
            {
                LogParser.DA.DataObjects.PlayerStatsDS.PlayerStatsTeamRow dr = _playerModel.GetPlayerTeamStatsRow( _logId, player.PlayerId );
                LogParser.DA.DataObjects.PlayerStatsDS.PlayerStatsTeamRow dr2 = ( LogParser.DA.DataObjects.PlayerStatsDS.PlayerStatsTeamRow ) ( dt.NewRow() );

                dr2.ItemArray = dr.ItemArray;
                dt.Rows.Add( dr2 );
            }

            this.teamStats.DataSource = dt;

            if ( !Page.IsPostBack )
                dt.DefaultView.Sort = "Points DESC";

            this.teamStats.DataBind();

        }


        protected void teamStats_Sorting( object sender, GridViewSortEventArgs e )
        {
            DataTable dataTable = teamStats.DataSource as DataTable;

            if ( dataTable != null )
            {
                //DataView dataView = new DataView( dataTable );
                //dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql( GridViewSortDirection );


                dataTable.DefaultView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql( GridViewSortDirection );
                teamStats.DataSource = dataTable;
                teamStats.DataBind();
            }



        }

        public SortDirection GridViewSortDirection
        {
            get
            {
                if ( ViewState[ "sortDirection" ] == null )
                    ViewState[ "sortDirection" ] = SortDirection.Ascending;

                SortDirection returnDirection = ( SortDirection ) ViewState[ "sortDirection" ];

                if ( ( SortDirection ) ViewState[ "sortDirection" ] == SortDirection.Ascending )
                    ViewState[ "sortDirection" ] = SortDirection.Descending;
                else
                    ViewState[ "sortDirection" ] = SortDirection.Ascending;



                return returnDirection;
            }
            set { ViewState[ "sortDirection" ] = value; }
        }

        private string ConvertSortDirectionToSql( SortDirection sortDirection )
        {
            string newSortDirection = String.Empty;

            switch ( sortDirection )
            {
                case SortDirection.Ascending:
                    newSortDirection = "ASC";
                    break;

                case SortDirection.Descending:
                    newSortDirection = "DESC";
                    break;
            }

            return newSortDirection;
        }
    }
}